package org.zhaozhen.pay.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhaozhen.basic.businessexception.BusinessException;
import org.zhaozhen.basic.service.impl.BaseServiceImpl;
import org.zhaozhen.order.service.IProductOrderService;
import org.zhaozhen.pay.domain.AlipayInfo;
import org.zhaozhen.pay.domain.PayBill;
import org.zhaozhen.pay.mapper.AlipayInfoMapper;
import org.zhaozhen.pay.mapper.PayBillMapper;
import org.zhaozhen.pay.service.IPayBillService;
import org.zhaozhen.pay.utils.AlipayUtils;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill payBill) {
        //判断参数是否为空
        if (payBill==null){
            throw new BusinessException("你传了个空的。。。");
        }
        PayBill payBill1 = payBillMapper.loadById(payBill.getId());
        if (payBill==null){
            throw new BusinessException("你又传了个空的。。。没有id");
        }
        Long payChannel = payBill.getPayChannel();
        switch(payChannel.intValue()){

            case 1 :
                //支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(payBill.getShop_id());
                return AlipayUtils.pay(info,payBill);
            //微信
            case 2 :
                //@TODO
                break;
            //银联
            case 3 :
                //@TODO
                break;
            //余额
            default :
                //@TODO
        }
        return null;
    }

    @Override
    public PayBill queryByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
